<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/12
 * Time: 9:31
 */

namespace app\admin\controller;


use pay\Ali;
use pay\Paypal;
use pay\Upacp;
use pay\Wx;
use think\Db;

class Order extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $order_status = input('order_status');
            $order_aftersale_status = input('order_aftersale_status');
            $goods_cate = input('goods_cate');
            $order_type = input('order_type');
            $create_start_time = input('create_start_time');
            $create_end_time = input('create_end_time');
            $goods_name = input('goods_name');
            $order_number = input('order_number');
            
            $where['order_del'] = 1;
            // 商品名称|约稿标题
            if(isset($goods_name) && $goods_name != ''){
                $where['goods_name|yue_title'] = ['like', '%' . $goods_name . '%'];
            }
            // 订单编号
            if(isset($order_number) && $order_number != ''){
                $where['order_number'] = ['like', '%' . $order_number . '%'];
            }
            // 订单状态
            if(isset($order_status) && $order_status != ''){
                $where['order_status'] = $order_status;
            }
            // 订单完成状态
            if(isset($order_aftersale_status) && $order_aftersale_status != ''){
                $where['order_aftersale_status'] = $order_aftersale_status;
            }
            // 商品类目
            if(isset($goods_cate) && $goods_cate != ''){
                $where['goods_cate'] = $goods_cate;
            }
            // 订单类型
            if(isset($order_type) && $order_type != ''){
                $where['order_type'] = $order_type;
            }
            // 创建时间
            if(isset($create_start_time) && $create_start_time != '' && isset($create_end_time) && $create_end_time != ''){
                $where['order_create_time'] = ['between', [$create_start_time, $create_end_time]];
            }
            if(isset($create_start_time) && $create_start_time != '' && isset($create_end_time) && $create_end_time == ''){
                $where['order_create_time'] = ['>', $create_start_time];
            }
            if(isset($create_start_time) && $create_start_time == '' && isset($create_end_time) && $create_end_time != ''){
                $where['order_create_time'] = ['<', $create_end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('goods_order')
                ->join('up_u_user s', 's.user_id = order_sail_user_id', 'LEFT')
                ->join('up_u_user b', 'b.user_id = order_buy_user_id', 'LEFT')
                ->join('up_goods', 'order_goods_id = goods_id', 'LEFT')
                ->join('up_goods_spec', 'order_spec_id = spec_id', 'LEFT')
                ->join('up_yue', 'order_yue_id = yue_id', 'LEFT')
                ->join('up_goods_type', 'gt_id = goods_cate', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('goods_order')
                ->join('up_u_user s', 's.user_id = order_sail_user_id', 'LEFT')
                ->join('up_u_user b', 'b.user_id = order_buy_user_id', 'LEFT')
                ->join('up_goods', 'order_goods_id = goods_id', 'LEFT')
                ->join('up_goods_spec', 'order_spec_id = spec_id', 'LEFT')
                ->join('up_yue', 'order_yue_id = yue_id', 'LEFT')
                ->join('up_goods_type', 'gt_id = goods_cate', 'LEFT')
                ->field('order_id, order_number, goods_name, goods_thumb, goods_sail_num, order_num, order_status, 
                order_pay_price, yue_title, spec_name, spec_can, order_pay_type, order_buy_user_id, order_sail_user_id, 
                s.user_username sail_username, s.user_phone sail_userphone, b.user_username buy_username, 
                b.user_phone buy_userphone, order_type, order_create_time, gt_name, order_aftersale_status')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('order_create_time asc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $cate = Db::table('up_goods_type')
            ->where('gt_del', 1)
            ->field('gt_id, gt_name')
            ->select();
        $this->assign('cate', $cate);
        return $this->fetch();
    }
    
    /**
     * 详情
     * @return mixed
     */
    public function orderDetail(){
        $order_id = input('order_id');
        $order = Db::table('up_goods_order')
            ->join('up_u_user s', 's.user_id = order_sail_user_id', 'LEFT')
            ->join('up_u_user b', 'b.user_id = order_buy_user_id', 'LEFT')
            ->join('up_goods', 'order_goods_id = goods_id', 'LEFT')
            ->join('up_goods_spec', 'order_spec_id = spec_id', 'LEFT')
            ->join('up_yue', 'order_yue_id = yue_id', 'LEFT')
            ->join('up_goods_type', 'gt_id = goods_cate', 'LEFT')
            ->field('order_id, order_number, goods_name, goods_thumb, goods_sail_num, order_num, order_status, 
                order_pay_price, yue_title, spec_name, spec_can, order_pay_type, order_buy_user_id, order_sail_user_id, 
                s.user_username sail_username, s.user_phone sail_userphone, b.user_username buy_username, 
                b.user_phone buy_userphone, order_type, order_create_time, gt_name, order_goods_price, order_aftersale_status')
            ->where('order_id', $order_id)
            ->find();
        $this->assign([
            'order' => $order
        ]);
        return $this->fetch('order/orderdetail');
    }
    
    /**
     * 退款
     * @return \think\response\Json
     */
    public function orderReturn(){
        $order_id = input('id');
        $order = Db::table('up_goods_order')
            ->where('order_id', $order_id)
            ->field('order_number, order_pay_type, order_num, order_type, order_spec_id, 
            order_goods_id, order_pay_price, order_return_number, order_USD_price')
            ->find();
        // TODO:其他类型退款
        $return_number = date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT); // 退款订单号
        if($order['order_pay_type'] == 1){ // 微信
            import('returnprice.Wx', EXTEND_PATH);
            $wx = new Wx(config('weixin'));
            $return = $wx->weixiReturn($order['order_pay_price'], $order['order_pay_price'], $order['order_number'], $return_number);
        }elseif($order['order_pay_type'] == 2){ // 支付宝
            import('returnprice.Ali', EXTEND_PATH);
            $ali = new Ali(config('ali'));
            $return = $ali->aliReturn($order['order_pay_price'], $order['order_number']);
        }elseif($order['order_pay_type'] == 3){ // 银联
            import('returnprice.Upacp', EXTEND_PATH);
            $upacp = new Upacp();
            $return = $upacp->yinLianRefund($return_number, $order['order_return_number'], $order['order_pay_price']);
        }elseif($order['order_pay_type'] == 4){ // visa
        
        }else{ // PayPal
            import('returnprice.Paypal', EXTEND_PATH);
            $payPal = new Paypal();
            $return = $payPal->returnPrice($order['order_USD_price'], $order['order_return_number']);
        }
    
        if($return['status'] == 200){
            Db::startTrans();
            try{
                $update = [
                    'order_status' => 6
                ];
                if($order['order_pay_type'] == 1){
                    $update['order_return_number'] = $return_number;
                }
                if($order['order_pay_type'] == 3){
                    $update['order_upacp_return_number'] = $return_number;
                }
                // 修改订单状态
                Db::table('up_goods_order')
                    ->where('order_id', $order_id)
                    ->update($update);
                // 正常商品、秒杀商品、拍卖商品，释放库存、减销量
                if($order['order_type'] == 1 || $order['order_type'] == 2 || $order['order_type'] == 3){
                    // 释放库存
                    Db::table('up_goods_spec')->where('spec_id', $order['order_spec_id'])->setInc('spec_stock', $order['order_num']);
                    // 减销量
                    Db::table('up_goods_spec')->where('spec_id', $order['order_spec_id'])->setDec('spec_sail_num', $order['order_num']);
                    Db::table('up_goods')->where('goods_id', $order['order_goods_id'])->setDec('goods_sail_num', $order['order_num']);
                }
                Db::commit();
                return json(['code' => 200, 'msg' => '操作成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['code' => 100, 'msg' => $e->getMessage()]);
            }
        }else{
            return json(['code' => 100, 'msg' => $return['msg']]);
        }
    }
}